/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { reactive, h } from 'vue'
import { NEllipsis, NIcon } from 'naive-ui'
import { useI18n } from 'vue-i18n'
import {
  HomeOutlined,
  ProfileOutlined,
  FolderOutlined,
  DatabaseOutlined,
  DesktopOutlined,
  SafetyCertificateOutlined,
  UserOutlined,
  LogoutOutlined,
  FundProjectionScreenOutlined,
  PartitionOutlined,
  SettingOutlined,
  FileSearchOutlined,
  RobotOutlined,
  AppstoreOutlined,
  UsergroupAddOutlined,
  UserAddOutlined,
  WarningOutlined,
  InfoCircleOutlined,
  ControlOutlined,
  SlackOutlined,
  EnvironmentOutlined,
  KeyOutlined,
  SafetyOutlined,
  GroupOutlined,
  ContainerOutlined,
  ApartmentOutlined,
  BarsOutlined,
  CloudServerOutlined,
  ScheduleFilled,
  MonitorOutlined,
  DatabaseFilled,
  PropertySafetyFilled,
  ApiFilled,
  MenuFoldOutlined,
  RollbackOutlined,
  SettingFilled
} from '@vicons/antd'
import { useRoute } from 'vue-router'
import { useUserStore } from '@/store/user/user'
import { timezoneList } from '@/common/timezone'
import type { UserInfoRes } from '@/service/modules/users/types'

export function useDataList() {
  const { t } = useI18n()
  const route = useRoute()
  const userStore = useUserStore()

  const renderIcon = (icon: any) => {
    return () => h(NIcon, null, { default: () => h(icon) })
  }

  const localesOptions = [
    {
      label: 'English',
      key: 'en_US'
    },
    {
      label: '中文',
      key: 'zh_CN'
    }
  ]

  const timezoneOptions = () => {
    // @ts-ignore
    timezoneList.map((item: any) => ({ label: item, value: item }))
  }

  const state = reactive({
    isShowSide: false,
    localesOptions,
    timezoneOptions: timezoneOptions(),
    userDropdownOptions: [],
    menuOptions: [],
    headerMenuOptions: [],
    sideMenuOptions: []
  })

  const changeMenuOption = (state: any) => {
    // @ts-ignore
    const projectCode = route.params.projectCode || ''
    state.menuOptions = [
      {
        label: () => h(NEllipsis, null, { default: () => t('menu.home') }),
        key: 'home',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'home'
        // ),
        icon: renderIcon(HomeOutlined)
      },
      {
        label: () => h(NEllipsis, null, { default: () => t('menu.project') }),
        key: 'projects',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'projects'
        // ),
        icon: renderIcon(ProfileOutlined),
        children: [
          {
            label: t('menu.project_overview'),
            key: `/projects/${projectCode}`,
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'projects:list'
            // ),
            icon: renderIcon(FundProjectionScreenOutlined)
          },
          {
            label: t('menu.workflow'),
            key: 'workflow',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'workflow'
            // ),
            icon: renderIcon(PartitionOutlined),
            children: [
              {
                label: t('menu.workflow_relation'),
                key: `/projects/${projectCode}/workflow/relation`,
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('projects:workflow:relation')
              },
              {
                label: t('menu.workflow_definition'),
                key: `/projects/${projectCode}/workflow-definition`,
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('projects:workflow-definition')
              },
              {
                label: t('menu.workflow_instance'),
                key: `/projects/${projectCode}/workflow/instances`,
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('projects:workflow:instances')
              }
            ]
          },
          {
            label: t('menu.task'),
            key: 'task',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'task'
            // ),
            icon: renderIcon(SettingOutlined),
            children: [
              {
                label: t('menu.task_definition'),
                key: `/projects/${projectCode}/task/definitions`,
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('projects:task:definitions')
              },
              {
                label: t('menu.task_instance'),
                key: `/projects/${projectCode}/task/instances`,
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('projects:task:instances')
              }
            ]
          }
        ]
      },
      {
        label: () => h(NEllipsis, null, { default: () => t('menu.resources') }),
        key: 'resource',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'resource'
        // ),
        icon: renderIcon(FolderOutlined),
        children: [
          {
            label: t('menu.file_manage'),
            key: '/resource/file-manage',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'resource:file-manage'
            // ),
            icon: renderIcon(FileSearchOutlined)
          },
          {
            label: t('menu.udf_manage'),
            key: 'udf-manage',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'udf-manage'
            // ),
            icon: renderIcon(RobotOutlined),
            children: [
              {
                label: t('menu.resource_manage'),
                key: '/resource/resource-manage',
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('resource:resource-manage')
              },
              {
                label: t('menu.function_manage'),
                key: '/resource/function-manage',
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('resource:function-manage')
              }
            ]
          },
          {
            label: t('menu.task_group_manage'),
            key: 'task-group-manage',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'task-group-manage'
            // ),
            icon: renderIcon(GroupOutlined),
            children: [
              {
                label: t('menu.task_group_option'),
                key: '/resource/task-group-option',
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('resource:task-group-option')
              },
              {
                label: t('menu.task_group_queue'),
                key: '/resource/task-group-queue',
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('resource:task-group-queue')
              }
            ]
          }
        ]
      },
      {
        label: () =>
          h(NEllipsis, null, { default: () => t('menu.data_quality') }),
        key: 'data-quality',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'data-quality'
        // ),
        icon: renderIcon(ContainerOutlined),
        children: [
          {
            label: t('menu.task_result'),
            key: '/data-quality/task-result',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'data-quality:task-result'
            // ),
            icon: renderIcon(ApartmentOutlined)
          },
          {
            label: t('menu.rule'),
            key: '/data-quality/rule',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'data-quality:rule'
            // ),
            icon: renderIcon(BarsOutlined)
          }
        ]
      },
      {
        label: () =>
          h(NEllipsis, null, { default: () => t('menu.datasource') }),
        key: 'datasource',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'datasource'
        // ),
        icon: renderIcon(DatabaseOutlined),
        children: []
      },
      {
        label: () => h(NEllipsis, null, { default: () => t('menu.monitor') }),
        key: 'monitor',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'monitor'
        // ),
        icon: renderIcon(DesktopOutlined),
        children: [
          {
            label: t('menu.service_manage'),
            key: 'service-manage',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'service-manage'
            // ),
            icon: renderIcon(AppstoreOutlined),
            children: [
              {
                label: t('menu.master'),
                key: '/monitor/master',
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('monitor:master')
              },
              {
                label: t('menu.worker'),
                key: '/monitor/worker',
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('monitor:worker')
              },
              {
                label: t('menu.db'),
                key: '/monitor/db',
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('monitor:db')
              }
            ]
          },
          {
            label: t('menu.statistical_manage'),
            key: 'statistical-manage',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'statistical-manage'
            // ),
            icon: renderIcon(AppstoreOutlined),
            children: [
              {
                label: t('menu.statistics'),
                key: '/monitor/statistics',
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('monitor:statistics')
              },
              {
                label: t('menu.audit_log'),
                key: '/monitor/audit-log',
                // @ts-ignore
                // show: (
                //   userStore.getUserInfo as UserInfoRes
                // ).permissions.includes('monitor:audit-log')
              }
            ]
          }
        ]
      },
      {
        label: () => h(NEllipsis, null, { default: () => t('menu.security') }),
        key: 'security',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'security'
        // ),
        icon: renderIcon(SafetyCertificateOutlined),
        children:
          (userStore.getUserInfo as UserInfoRes).userType === 'ADMIN_USER' ?
            [
            {
              label: t('menu.tenant_manage'),
              key: '/security/tenant-manage',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //   'security:tenant-manage'
              // ),
              icon: renderIcon(UsergroupAddOutlined)
            },
            {
              label: t('menu.user_manage'),
              key: '/security/user-manage',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //   'security:user-manage'
              // ),
              icon: renderIcon(UserAddOutlined)
            },
            {
              label: t('menu.system_organization'),
              key: '/security/system-organization',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //   'system:organization'
              // ),
              icon: renderIcon(PropertySafetyFilled)
            },
            {
              label: t('menu.alarm_group_manage'),
              key: '/security/alarm-group-manage',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //   'security:alarm-group-manage'
              // ),
              icon: renderIcon(WarningOutlined)
            },
            {
              label: t('menu.alarm_instance_manage'),
              key: '/security/alarm-instance-manage',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //   'security:alarm-instance-manage'
              // ),
              icon: renderIcon(InfoCircleOutlined)
            },
            {
              label: t('menu.worker_group_manage'),
              key: '/security/worker-group-manage',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //   'security:worker-group-manage'
              // ),
              icon: renderIcon(ControlOutlined)
            },
            {
              label: t('menu.yarn_queue_manage'),
              key: '/security/yarn-queue-manage',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //   'security:yarn-queue-manage'
              // ),
              icon: renderIcon(SlackOutlined)
            },
            {
              label: t('menu.environment_manage'),
              key: '/security/environment-manage',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //   'security:environment-manage'
              // ),
              icon: renderIcon(EnvironmentOutlined)
            },
            {
              label: t('menu.k8s_namespace_manage'),
              key: '/security/k8s-namespace-manage',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //   'security:k8s-namespace-manage'
              // ),
              icon: renderIcon(CloudServerOutlined)
            },
            {
              label: t('menu.token_manage'),
              key: '/security/token-manage',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //   'security:token-manage'
              // ),
              icon: renderIcon(SafetyOutlined)
            }
          ] : [
            {
              label: t('menu.token_manage'),
              key: '/security/token-manage',
              // @ts-ignore
              // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
              //     'security:token-manage'
              // ),
              icon: renderIcon(SafetyOutlined)
            }
          ]
      },
      {
        label: () =>
          h(NEllipsis, null, { default: () => t('menu.integration') }),
        key: 'integration',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'integration'
        // ),
        icon: renderIcon(ScheduleFilled),
        children: [
          {
            label: t('menu.integration_assets'),
            key: '/integration/assets',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'integration:assets'
            // ),
            icon: renderIcon(PropertySafetyFilled)
          },
          {
            label: t('menu.integration_govern'),
            key: '/integration/govern',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'integration:govern'
            // ),
            icon: renderIcon(DatabaseFilled)
          },
          {
            label: t('menu.integration_monitor'),
            key: '/integration/monitor',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'integration:monitor'
            // ),
            icon: renderIcon(MonitorOutlined)
          },
          // {
          //   label: t('menu.integration_schedule'),
          //   key: '/integration/schedule',
          //   // @ts-ignore
          //   show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
          //     'integration:schedule'
          //   ),
          //   icon: renderIcon(ScheduleFilled)
          // }
          {
            label: t('menu.integration_sqlmanage'),
            key: '/integration/sqlmanage',
            // @ts-ignore
            // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
            //   'integration:sqlmanage'
            // ),
            icon: renderIcon(ScheduleFilled)
          }
        ]
      },
      {
        label: () => h(NEllipsis, null, { default: () => t('menu.atlas') }),
        key: 'atlas',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'atlas'
        // ),
        icon: renderIcon(ApiFilled),
        children: []
      },
      // {
        // label: () => h(NEllipsis, null, { default: () => t('menu.system') }),
        // key: 'system',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'system'
        // ),
        // icon: renderIcon(SettingFilled),
        // children: [
          // {
          //   label: t('menu.system_organization'),
          //   key: '/system/organization',
          //   // @ts-ignore
          //   // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
          //   //   'system:organization'
          //   // ),
          //   icon: renderIcon(PropertySafetyFilled)
          // },
          // {
          //   label: t('menu.system_role'),
          //   key: '/system/role',
          //   // @ts-ignore
          //   show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
          //     'system:role'
          //   ),
          //   icon: renderIcon(RollbackOutlined)
          // },
          // {
          //   label: t('menu.system_menus'),
          //   key: '/system/menus',
          //   // @ts-ignore
          //   show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
          //     'system:menus'
          //   ),
          //   icon: renderIcon(MenuFoldOutlined)
          // },
          // {
          //   label: t('menu.system_user'),
          //   key: '/system/user',
          //   // @ts-ignore
          //   show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
          //     'system:user'
          //   ),
          //   icon: renderIcon(UserAddOutlined)
          // },
          // {
          //   label: t('menu.system_log'),
          //   key: '/system/log',
          //   // @ts-ignore
          //   show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
          //     'system:log'
          //   ),
          //   icon: renderIcon(UserAddOutlined)
          // }
      //   ]
      // }
    ]
    state.menuOptions = state.menuOptions.map(
      (item: {
        label: string
        key: string
        icon: any
        show: boolean
        children: any
      }) => {
        if (item.show) {
          return {
            label: item.label,
            key: item.key,
            icon: item.icon,
            show: item.show,
            children: item.children
          }
        } else {
          return {
            label: item.label,
            key: item.key,
            icon: item.icon,
            show: item.show,
            children: item.children
          }
        }
      }
    )
  }

  const changeHeaderMenuOptions = (state: any) => {
    state.headerMenuOptions = state.menuOptions.map(
      (item: { label: string; key: string; icon: any; show: boolean }) => {
        if (item.show) {
          return {
            label: item.label,
            key: item.key,
            icon: item.icon,
            show: item.show
          }
        } else {
          return {
            label: item.label,
            key: item.key,
            icon: item.icon,
            show: item.show
          }
        }
      }
    )
  }

  const changeUserDropdown = (state: any) => {
    state.userDropdownOptions = [
      {
        label: t('user_dropdown.profile'),
        key: 'profile',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'profile'
        // ),
        icon: renderIcon(UserOutlined)
      },
      {
        label: t('user_dropdown.password'),
        key: 'password',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'password'
        // ),
        icon: renderIcon(KeyOutlined)
      },
      {
        label: t('user_dropdown.logout'),
        key: 'logout',
        // @ts-ignore
        // show: (userStore.getUserInfo as UserInfoRes).permissions.includes(
        //   'logout'
        // ),
        icon: renderIcon(LogoutOutlined)
      }
    ]
  }

  return {
    state,
    changeHeaderMenuOptions,
    changeMenuOption,
    changeUserDropdown
  }
}
